package com.chuangmei.com.arindoorlocationsystem;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.chuangmei.com.arindoorlocationsystem.base.adapter.VpAdapter;
import com.chuangmei.com.arindoorlocationsystem.base.BaseActivity;
import com.chuangmei.com.arindoorlocationsystem.home.HomeActivity;
import com.chuangmei.com.arindoorlocationsystem.utils.CommonUtils;
import com.chuangmei.com.arindoorlocationsystem.utils.Constant;
import com.chuangmei.com.arindoorlocationsystem.utils.SharedPreferencesUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class SplashActivity extends BaseActivity {
    @BindView(R.id.mVP_splash)
    ViewPager mVP_splash;
    @BindView(R.id.mBT_start_home_activity)
    Button mBT_start_home_activity;
    @BindView(R.id.mLL_mini_dian)
    LinearLayout mLL_mini_dian;
    @BindView(R.id.mRL_mini_move_point)
    RelativeLayout mRL_mini_move_point;


    private ArrayList<ImageView> imageViews;
    List<String> mPermissionList = new ArrayList<>();
    private ImageView miniMoveImageView;

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_splash);
        //初始化应用文件夹
        CommonUtils.CheckSystemDir();
        //判定是否是首次登录
        if (SharedPreferencesUtils.getUserAppIsFirstLoginMessage("isFirstLogin")) {
        } else {
            Intent intent = new Intent(this, HomeActivity.class);
            startActivity(intent);
            finish();
        }

    }

    @Override
    protected void initView() {
        mVP_splash = (ViewPager) this.findViewById(R.id.mVP_splash);
        mBT_start_home_activity = (Button) this.findViewById(R.id.mBT_start_home_activity);
        mLL_mini_dian = (LinearLayout) this.findViewById(R.id.mLL_mini_dian);
        mRL_mini_move_point = (RelativeLayout) this.findViewById(R.id.mRL_mini_move_point);
        initMiniPoint();
        initImages();
        initButton();
    }

    /**
     * 点击跳转主页面
     */
    private void initButton() {
        mBT_start_home_activity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(SplashActivity.this, HomeActivity.class);
                startActivity(intent);
                SharedPreferencesUtils.isUserFirstLogin("isFirstLogin", false);
                finish();
            }
        });
    }

    @Override
    protected void initData() {
        super.initData();
        initPermission();
    }

    /**
     * ViewPager初始化填充图片
     */
    private void initImages() {
        //设置每一张图片都填充窗口
        ViewPager.LayoutParams mParams = new ViewPager.LayoutParams();
        imageViews = new ArrayList<ImageView>();

        for (int i = 0; i < Constant.splashImgs.length; i++) {
            ImageView iv = new ImageView(this);
            iv.setLayoutParams(mParams);//设置布局
            iv.setImageResource(Constant.splashImgs[i]);//为ImageView添加图片资源
            iv.setScaleType(ImageView.ScaleType.FIT_XY);//这里也是一个图片的适配
            imageViews.add(iv);
        }
        VpAdapter vpAdapter = new VpAdapter(imageViews);
        mVP_splash.setAdapter(vpAdapter);
        mVP_splash.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                movePoint(position, positionOffset);
            }

            @Override
            public void onPageSelected(int position) {
                if (position == Constant.splashImgs.length - 1) {
                    mBT_start_home_activity.setVisibility(View.VISIBLE);
                } else {
                    mBT_start_home_activity.setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    /**
     * 指标小圆点移动，40是两个点之间的Margins，20是第一个点最左边的Margins
     *
     * @param position       现在是哪个图片
     * @param positionOffset 移动到下张图的比例
     */
    private void movePoint(int position, float positionOffset) {
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.dark_point);
        int scale = bitmap.getWidth() + 40;
        float marginsLeft = position * scale + positionOffset * scale;
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) miniMoveImageView.getLayoutParams();
        params.setMargins((int) marginsLeft + 20, 0, 0, 0);
        miniMoveImageView.setLayoutParams(params);
    }

    /**
     * 初始化三个指标白点跟移动的黑点
     */
    private void initMiniPoint() {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams
                .WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(20, 0, 20, 0);
        for (int i = 0; i < Constant.splashImgs.length; i++) {
            ImageView imageView = new ImageView(this);
            imageView.setLayoutParams(layoutParams);
            imageView.setImageResource(R.mipmap.light_color_point);
            mLL_mini_dian.addView(imageView);
        }

        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.dark_point);
        RelativeLayout.LayoutParams mRL_mini_move_pointLayoutParams = (RelativeLayout.LayoutParams)
                mRL_mini_move_point.getLayoutParams();
        mRL_mini_move_pointLayoutParams.width = bitmap.getWidth() * 3 + 20 * 6;
        mRL_mini_move_pointLayoutParams.height = bitmap.getHeight();
        mRL_mini_move_point.setLayoutParams(mRL_mini_move_pointLayoutParams);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams
                .WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.setMargins(20, 0, 0, 0);
        miniMoveImageView = new ImageView(this);
        miniMoveImageView.setImageResource(R.mipmap.dark_point);
        miniMoveImageView.setLayoutParams(params);
        mRL_mini_move_point.addView(miniMoveImageView);
    }

    /**
     * 6.0以后请求SD卡权限
     */
    private void initPermission() {
        mPermissionList.clear();
        for (int i = 0; i < Constant.requestPermission.length; i++) {
            if (ContextCompat.checkSelfPermission(this, Constant.requestPermission[i]) != PackageManager
                    .PERMISSION_GRANTED) {
                mPermissionList.add(Constant.requestPermission[i]);
            }
        }
        if (mPermissionList.isEmpty()) {//未授予的权限为空，表示都授予了
            Toast.makeText(this, "已经授权", Toast.LENGTH_LONG).show();
        } else {//请求权限方法
            String[] permissions = mPermissionList.toArray(new String[mPermissionList.size()]);//将List转为数组
            ActivityCompat.requestPermissions(this, permissions, 101);
        }
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {

        if (requestCode == 101) {
            for (int i = 0; i < grantResults.length; i++) {
                if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
                    //判断是否勾选禁止后不再询问
                    boolean showRequestPermission = ActivityCompat.shouldShowRequestPermissionRationale(this,
                            permissions[i]);
                    if (showRequestPermission) {
                        showToast("未授予权限");
                        finish();
                    }
                }
            }
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

}
